package model

import (
	"fmt"
	"usplatformserver/pkg/app"

	"github.com/jinzhu/gorm"
)

var (
	ToBeAnalyzed      = 1 //待分析
	Analyzing         = 2 //正在分析
	AnalysisCompleted = 3 //分析完成
	AnalysisFailed    = 4 //分析失败
)

type UploadFileInfo struct {
	*Model
	FileName   string `json:"fileName" gorm:"Column:fileName"`
	FilePath   string `json:"filePath" gorm:"Column:filePath"`
	FileSize   string `json:"fileSize" gorm:"Column:fileSize"`
	Status     int    `json:"status" gorm:"Column:status"`
	UploadTime string `json:"uploadTime" gorm:"Column:uploadTime" sql:"-"`
	OrganType  int    `json:"organType" gorm:"Column:organType"`
}

type UploadFilePage struct {
	Page int `json:"page" gorm:"Column:page"`
}

func (u UploadFileInfo) TableName() string {
	return "t_video_file_info"
}

type uploadFileListInfo struct {
	List  []*UploadFileInfo
	Pager *app.Pager
}

type UploadFileListSwagger struct {
	*SwaggerSuccess
	Data *uploadFileListInfo `json:"data"`
}

type UploadFileSuccess struct {
	*SwaggerSuccess
	Data *Model `json:"data"`
}

type UploadFileHighLightSuccess struct {
	*SwaggerSuccess
	Data *UploadFilePage `json:"data"`
}

/**
 * @brief 插入数据
 * @param db-数据库访问实例
 * @return 文件ID，错误信息
 */
func (info UploadFileInfo) InsertUploadFile(db *gorm.DB) (int, error) {
	res := db.Table(info.TableName()).Create(&info)
	if res.Error != nil {
		return -1, res.Error
	}
	var uploads []*UploadFileInfo
	res = db.Table(info.TableName()).Where(`fileName = ? and isDelete = 0`, info.FileName).Order("id desc").Find(&uploads)
	if res.Error != nil {
		return -1, res.Error
	}
	if len(uploads) > 0 {
		return int(uploads[0].ID), nil
	}

	return -1, nil
}

func (info UploadFileInfo) DeleteUploadFile(db *gorm.DB, fileId int) error {
	var query = fmt.Sprintf("delete from %v where id = %v", info.TableName(), fileId)
	res := db.Exec(query)
	if res.Error != nil {
		return res.Error
	}

	return nil
}

/**
 * @brief 获取上传文件信息列表
 * @param db-数据库访问实例
 * @param organType-检查类型
 * @param option-搜索条件
 * @param beginTime-起始时间
 * @param endTime-结束时间
 * @param pageOffset-当前页起始数据偏移
 * @param pageSize-页码
 * @return 错误信息
 */
func (info UploadFileInfo) List(db *gorm.DB, organType uint32, option, beginTime, endTime string, pageOffset, pageSize int) ([]*UploadFileInfo, error) {
	var uploads []*UploadFileInfo

	var query string
	query = "SELECT id, fileName, filePath, fileSize, status, createTime as uploadTime"
	query += " FROM t_video_file_info"
	query += fmt.Sprintf(" WHERE isDelete=0 and organType=%v", organType)
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND createTime >= "%s" AND createTime <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(fileName) like "%%%s%%"`, option)
	}
	//排序
	query += " order by id desc"
	//分页
	if pageOffset >= 0 && pageSize > 0 {
		query += fmt.Sprintf(" LIMIT %d, %d", pageOffset, pageSize)
	}

	res := db.Raw(query).Scan(&uploads)
	if res.Error != nil {
		return nil, res.Error
	}

	return uploads, nil
}

/**
 * @brief 获取上传文件信息列表总数
 * @param db-数据库访问实例
 * @param organType-检查类型
 * @param option-搜索条件
 * @param beginTime-起始时间
 * @param endTime-结束时间
 * @return 错误信息
 */
func (info UploadFileInfo) Count(db *gorm.DB, organType uint32, option, beginTime, endTime string) (int, error) {
	var count int
	var query string
	query = "SELECT count(id) as count FROM t_video_file_info t1"
	query += fmt.Sprintf(" WHERE isDelete=0 AND organType = %d", organType)
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND createTime >= "%s" AND createTime <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(fileName) like "%%%s%%"`, option)
	}

	res := db.Raw(query).Count(&count)
	if res.Error != nil {
		return -1, res.Error
	}

	return count, nil
}

/**
 * @brief 文件是否存在
 * @param db-数据库访问实例
 * @param fileName-文件名
 * @return 错误信息
 */
func (info UploadFileInfo) IsExistFile(db *gorm.DB, fileName string) (bool, error) {
	var count int
	query := "select count(id) as count from t_video_file_info "
	query += fmt.Sprintf(`where fileName="%s" `, fileName)
	query += "and isDelete=0"

	res := db.Raw(query).Count(&count)
	if res.Error != nil {
		return false, res.Error
	}

	return count > 0, nil
}
